﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using Microsoft.IdentityModel.Protocols.WSTrust;
using System.Xml;

namespace SlamCms.SharePointServices.Authentication
{
	//See http://fredericloud.com/2011/01/11/connecting-to-sharepoint-with-claims-authentication/

	internal class TrustClientRequestBodyWriter : BodyWriter
	{
		WSTrustRequestSerializer _serializer;
		RequestSecurityToken _rst;

		/// <summary>
		/// Constructs the Body Writer.
		/// </summary>
		/// <param name="serializer">Serializer to use for serializing the rst.</param>
		/// <param name="rst">The RequestSecurityToken object to be serialized to the outgoing Message.</param>
		public TrustClientRequestBodyWriter(WSTrustRequestSerializer serializer, RequestSecurityToken rst)
			: base(false)
		{
			if (serializer == null)
				throw new ArgumentNullException("serializer");

			_serializer = serializer;
			_rst = rst;
		}


		/// <summary>
		/// Override of the base class method. Serializes the rst to the outgoing stream.
		/// </summary>
		/// <param name="writer">Writer to which the rst should be written.</param>
		protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
		{
			_serializer.WriteXml(_rst, writer, new WSTrustSerializationContext());
		}
	}
}
